/*
 * Group 3, User Interface Programming II (vt11)
 * Pooya | Flora | Monica | Can
 */
package vical.utility;

import java.awt.Color;
import java.awt.Component;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

/**
 * This class contains the driver of all animations in this application.
 *
 * @author WoKe
 * @author Can
 */
public class AnimationDriver {

    private AnimationDriver() {
    }

    /**
     * Does flashing for the specified component with specified timespan
     * @param component
     * @param timeSpan
     */
    public static void flash(final Component component, final int timeSpan) {
        Runnable r = new Runnable() {

            public void run() {
                try {
                    component.setVisible(false);
                    Thread.sleep(timeSpan);
                    component.setVisible(true);
                } catch (InterruptedException x) {
                    System.out.println(x.getLocalizedMessage());
                    Thread.currentThread().interrupt();
                }
            }
        };

        Thread thread = new Thread(r);
        thread.start();
    }

    /**
     * Highlights specified component
     * @param component
     * @param timeSpan
     * @param howManyTimes
     */
    public static void highlight(final Component component, final int timeSpan, final int howManyTimes) {
        Runnable r = new Runnable() {

            public void run() {
                try {
                    doHighlight(component, timeSpan, howManyTimes);
                } catch (Exception x) {
                    System.out.println(x.getLocalizedMessage());
                    Thread.currentThread().interrupt();
                }
            }
        };

        Thread thread = new Thread(r);
        thread.start();
    }

    private static void doHighlight(Component component, int timeSpan, int howManyTimes) {
        Color oldColor = component.getBackground();
        try {
            for (int i = 0; i < howManyTimes; i++) {
                component.setBackground(new Color(245, 224, 225));
                Thread.sleep(timeSpan);
                component.setBackground(new Color(245, 224, 200));
                Thread.sleep(timeSpan);
                component.setBackground(new Color(245, 224, 180));
                Thread.sleep(timeSpan);
                component.setBackground(new Color(245, 222, 100));
                Thread.sleep(300);
                component.setBackground(new Color(245, 224, 180));
                Thread.sleep(timeSpan);
                component.setBackground(new Color(245, 224, 200));
                Thread.sleep(timeSpan);
                component.setBackground(new Color(245, 224, 224));
                Thread.sleep(timeSpan);
            }
        } catch (InterruptedException x) {
            Thread.currentThread().interrupt();
        }
        component.setBackground(oldColor);
    }

    public static void Shine(final Component component) {
        JFrame frame = (JFrame) SwingUtilities.getRoot(component);
        MyGlassPane glass = new MyGlassPane(component, frame.getGlassPane().getGraphics());
        frame.setGlassPane(glass);
        glass.setVisible(true);
    }
}
